﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SportsStore.Data;
using SportsStore.Models.ViewModels;

namespace SportsStore.Controllers
{
    public class ProductController : Controller
    {
        private readonly IProductRepository _repository;
        public int PageSize = 4;

        public ProductController(IProductRepository repo)
        {
            _repository = repo;
        }
        public IActionResult Index(string category,int productPage=1)
        {
            return View(new ProductsListViewModel{
                Products = _repository.Products
                    .Where(p=>category==null||p.Category==category)
                    .OrderBy(p => p.Id).Skip((productPage - 1) * PageSize)
                    .Take(PageSize),
                PagingInfo = new PagingInfo()
                {
                    CurrentPage = productPage,
                    ItemsPerPage = PageSize,
                    TotalItems = _repository.Products.Where(p => category == null || p.Category == category).Count()
                },
                CurrentCategory=category
            });
        }
    }
}